package com.anthonybennis.blender.client.pages.keyboard;

import java.util.HashMap;
import java.util.Set;

import com.anthonybennis.blender.client.pages.keyboard.TilePanel.PANEL;
import com.google.gwt.dom.client.Style.Cursor;
import com.google.gwt.dom.client.Style.FontWeight;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.TouchStartEvent;
import com.google.gwt.event.dom.client.TouchStartHandler;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;

/**
 * 
 * @author abennis
 */
public class NavigationPanel 
{
	private static final String LIGHT_ORANGE = "rgb(255,205,85)";
	final TilePanel _tilePanel; // This NavigationPanel controls the TilePanel.
	private final HashMap<PANEL, Label> _navigationLabelMap;
	private String _backgroundColour;
	
	public NavigationPanel(TilePanel tilePanel)
	{
		_tilePanel = tilePanel;
		_navigationLabelMap = new HashMap<TilePanel.PANEL, Label>();
	}
	/**
	 * 
	 * @return
	 */
	protected FlowPanel create()
	{
		FlowPanel navigationPanel = new FlowPanel();
		navigationPanel.setStyleName("nav-panel");
		navigationPanel.getElement().setId("nav-panel");
		_backgroundColour = navigationPanel.getElement().getStyle().getBackgroundColor();
		
		this.createHyperLink(PANEL.GENERAL, navigationPanel);
		this.createHyperLink(PANEL.VIEW, navigationPanel);
		this.createHyperLink(PANEL.EDIT, navigationPanel);
		this.createHyperLink(PANEL.OBJECT, navigationPanel);
		
		return navigationPanel;
	}
	
	/**
	 * 
	 * @param panel
	 */
	protected void highlightNavigationButton(PANEL panel)
	{
		Set<PANEL> panelKeys = _navigationLabelMap.keySet();
		Label childLabel;
		
		/*
		 * Reset all LABELS
		 */
		for (PANEL panelKey : panelKeys) 
		{
			childLabel = _navigationLabelMap.get(panelKey);
			
			if (childLabel instanceof Label)
			{
				childLabel.getElement().getStyle().setFontWeight(FontWeight.NORMAL);
				childLabel.getElement().getStyle().setBackgroundColor(_backgroundColour);
			}
		}
		
		this.highlightSelectedLabel(panel);
	}
	
	private void highlightSelectedLabel(PANEL panelKey)
	{
		Label childLabel = _navigationLabelMap.get(panelKey);
		childLabel.getElement().getStyle().setFontWeight(FontWeight.BOLD);
		childLabel.getElement().getStyle().setBackgroundColor(LIGHT_ORANGE);
	}
	
	/**
	 * 
	 * @param name
	 * @param parent
	 * @return
	 */
	private Label createHyperLink(final PANEL panel, final ComplexPanel parent)
	{
		final Label navigationLabel = new Label();
		
		navigationLabel.setStyleName("nav-label");
		navigationLabel.setText(panel.name());
		navigationLabel.getElement().getStyle().setCursor(Cursor.POINTER);
		navigationLabel.getElement().setAttribute("align", "center");
		
		/*
		 * For Mouse events.
		 */
		navigationLabel.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				_tilePanel.show(panel);
				highlightNavigationButton(panel);
			}
		});
		
		/*
		 * For Mobile apps
		 * This is needed as ClickHandler will not work on Android Devices.
		 */
		navigationLabel.addTouchStartHandler(new TouchStartHandler() {
			
			@Override
			public void onTouchStart(TouchStartEvent event) {
				_tilePanel.show(panel);
				highlightNavigationButton(panel);
			}
		});

		parent.add(navigationLabel);
		_navigationLabelMap.put(panel, navigationLabel);
		
		return navigationLabel;
	}
}